package com.tianqu.tms.www.proxymember.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.CarStyle;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.ProxyCarInfo;
import com.tianqu.tms.www.proxymember.action.ProxyMemberCarDetailAction;
import com.tianqu.tms.www.proxymember.viewhelper.ProxyMemberCarSearchInputViewHelper;

/**
 * @author zhangxueliang
 *待办车辆信息查看（已受理）controller
 */
public class ProxyMemberCarDetailController extends TmsController{
	private Logger logger = Logger
	.getLogger(ProxyMemberCarDetailController.class);
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();
		
		try{
			controller.begin();
			String serial = (String)uc.getAttribute(ProxyMemberCarDetailAction.SERIAL);
			ProxyCarInfo proxyCarInfo = ProxyCarInfo.getInstance();
			proxyCarInfo = proxyCarInfo.findProxyCarBySerial(serial);
			ProxyMemberCarSearchInputViewHelper viewHelper = new ProxyMemberCarSearchInputViewHelper();
			toViewHelper(proxyCarInfo,viewHelper);
			viewHelper.setSerial(serial);
			uc.setAttribute(TmsConstants.VIEWHELP, viewHelper);
			
			controller.commit();
		}catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
		
	}
	
	
	/**
	 * entity -> viewhelper
	 * @param entity
	 * @return
	 * @throws TmsDaoException 
	 */
	private void toViewHelper(ProxyCarInfo entity,ProxyMemberCarSearchInputViewHelper viewHelper) throws TmsDaoException{
		Codes code = Codes.getInstance();
		CarStyle carStyle = CarStyle.getInstance();
		viewHelper.setCarName(carStyle.getCarStyleRealNameById(entity.getCarStyleId()));
		viewHelper.setBrandId(entity.getBrandId());
		viewHelper.setVendorId(entity.getVendorId());
		viewHelper.setCarStyleId(entity.getCarStyleId());		
		viewHelper.setLicensePlate(entity.getLicensePlate());
		viewHelper.setCarOwner(entity.getCarOwner());
		viewHelper.setCarOwnerIdCardNo(entity.getCarOwnerIdCardNo());		
		viewHelper.setCompanyContactPerson(entity.getCompanyContactPerson());	
		viewHelper.setEngine(entity.getEngine());		
		viewHelper.setJqx(entity.getJqx());
		if (entity.getLoadquality() == 0) {
			viewHelper.setLoadquality("");
		} else {
			viewHelper.setLoadquality(Integer.toString(entity.getLoadquality()));
		}
	
		if (entity.getMileage() == 0) {
			viewHelper.setMileage("");
		} else {
			viewHelper.setMileage(Integer.toString(entity.getMileage()));
		}
		viewHelper.setModelCode(entity.getModelCode());
		viewHelper.setOwnerAddress(entity.getOwnerAddress());
		viewHelper.setOwnerOrgNo(entity.getOwnerOrgNo());
		viewHelper.setOwnerPhone(entity.getOwnerPhone());
		
		if (entity.getPassenger() == 0) {
			viewHelper.setPassenger("");
		} else {
			viewHelper.setPassenger(Integer.toString(entity.getPassenger()));
		}
		if (entity.getPrice() == 0) {
			viewHelper.setPrice("");
		} else {
			viewHelper.setPrice(Integer.toString(entity.getPrice()));
		}
		viewHelper.setRegDate(entity.getRegDate());
		viewHelper.setRegisterCardNo(entity.getRegisterCardNo());
		viewHelper.setSendCardDate(entity.getSendCardDate());
		viewHelper.setSerial(entity.getSerial());
		viewHelper.setVendorCode(entity.getVendorCode());
		viewHelper.setYearCarefulPeriod(entity.getYearCarefulPeriod());	
		viewHelper.setCardType(code.getItemValueByItemIdAndItemCode(Codes.PLATE_TYPE, entity.getCardType()));		
		viewHelper.setCarType(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_TYPE,entity.getCarType()));
		viewHelper.setCarUse(code.getItemValueByItemIdAndItemCode(Codes.USER_NATURE,entity.getCarUse()));
		viewHelper.setColor(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_COLOR,entity.getColor()));
		viewHelper.setCountryStyle(code.getItemValueByItemIdAndItemCode(Codes.VEHICLE_COUNTRY,entity.getCountryStyle()));
		viewHelper.setVin(entity.getVin());
		
		
		viewHelper.setDisplacement(String.valueOf(entity.getDisplacement()));
		viewHelper.setGearbox(code.getItemValueByItemIdAndItemCode(Codes.GEARBOX_TYPE,entity.getGearbox()));
		viewHelper.setFuelType(code.getItemValueByItemIdAndItemCode(Codes.FUEL_TYPE,entity.getFuelType()));
		viewHelper.setOwnerType(code.getItemValueByItemIdAndItemCode(Codes.CAR_OWNER,entity.getOwnerType()));
		viewHelper.setStatus(code.getItemValueByItemIdAndItemCode(Codes.PROXYCAR_STATUS,entity.getStatus()));
		viewHelper.setGainType(String.valueOf(entity.getGainType()));
		viewHelper.setTransferType(String.valueOf(entity.getTransferType()));
		viewHelper.setNewOwnerType(String.valueOf(entity.getNewOwnerType()));
		
		viewHelper.setAgentName(entity.getAgentName());
		viewHelper.setAgentPhone(entity.getAgentPhone());
		viewHelper.setAgentAddress(entity.getAgentAddress());
		viewHelper.setAcceptedTime(String.valueOf(entity.getAcceptedTime()));
		viewHelper.setAgentOwnerIdCardNo(entity.getAgentOwnerIdCardNo());
		
		viewHelper.setTaxNo(entity.getTaxNo());
		viewHelper.setNewcarOwnerIdCardNo(entity.getNewcarOwnerIdCardNo());
		viewHelper.setCarMarketIn(entity.getCarMarketIn());
		viewHelper.setMoveOutCity(entity.getMoveOutCity());
		viewHelper.setMoveOutProvince(entity.getMoveOutProvince());
		viewHelper.setNewAgentAddress(entity.getNewAgentAddress());
		viewHelper.setNewAgentName(entity.getNewAgentName());
		viewHelper.setNewAgentOwnerIdCardNo(entity.getNewAgentOwnerIdCardNo());
		viewHelper.setNewAgentPhone(entity.getNewAgentPhone());
		viewHelper.setMailAddress(entity.getMailAddress());
		viewHelper.setZipCode(entity.getZipCode());
		viewHelper.setPaymentPeople(entity.getPaymentPeople());
		viewHelper.setNewOwnerAddress(entity.getNewOwnerAddress());
		viewHelper.setNewOwnerOrgNo(entity.getNewOwnerOrgNo());
		viewHelper.setNewOwnerPhone(entity.getNewOwnerPhone());
		viewHelper.setNewCarOwner(entity.getNewCarOwner());
	}

}
